package com.gmu.service.excel.Impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmu.listener.TeacherExcelListener;
import com.gmu.listener.IdExcelListener;
import com.gmu.mapper.excel.ExcelMapper;
import com.gmu.mapper.excel.TeacherExcelMapper;
import com.gmu.pojo.TblTeacher;
import com.gmu.pojo.excel.*;
import com.gmu.service.TblTeacherService;
import com.gmu.service.TblUnitsService;
import com.gmu.service.TblUserService;
import com.gmu.service.excel.TeacherExcelService;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.function.Function;

@Service
public class TeacherExcelServiceImpl extends ServiceImpl<TeacherExcelMapper, TeacherUserData> implements TeacherExcelService {

    @Resource
    private TblTeacherService teacherService;

    @Override
    public void saveFile(MultipartFile file, TblUserService userService, TblTeacherService teacherService, TblUnitsService unitsService, PasswordEncoder passwordEncoder) {
      //excel将数据导入数据库
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, TeacherUserData.class,new TeacherExcelListener(userService,teacherService,unitsService,passwordEncoder)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //将数据库导出excel
    @Override
    public void outDataBase(HttpServletResponse response) throws IOException {
        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 设置防止中文名乱码
        String filename = URLEncoder.encode("老师信息", "UTF-8");
//        //建议加上该段，否则可能会出现前端无法获取Content-disposition
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        // 文件下载方式(附件下载还是在当前浏览器打开)
        response.setHeader("Content-disposition", "attachment;filename=" +
                filename + ".xlsx");
        //统计student表中的所有数据
        List<TblTeacher> list = teacherService.list();
        //存放studentVo数据
        List<TeacherVo> l = new ArrayList<>();
        //一条一条的加进数据库
        for(int i = 0;i<list.size();i++){
            TeacherVo teacherVo = new TeacherVo();
//            teacherVo.setTeacherNumber(list.get(i).getTeacherNumber());
//            teacherVo.setName(list.get(i).getName());
//            teacherVo.setTelephone(list.get(i).getTelephone());
//            teacherVo.setSex(list.get(i).getSex());
//            teacherVo.setIdNumber(list.get(i).getIdNumber());
//            teacherVo.setSchool(list.get(i).getSchool());
//            teacherVo.setAcademy(list.get(i).getAcademy());
//            teacherVo.setDepartment(list.get(i).getDepartment());
//            teacherVo.setUnitsId(list.get(i).getUnitsId());
//            teacherVo.setAddress(list.get(i).getAddress());
            BeanUtils.copyProperties(list.get(i),teacherVo);
            l.add(teacherVo);
        }

        //调用easyexcel里面的方法实现写入操作
        EasyExcel.write(response.getOutputStream(),TeacherVo.class).sheet("老师列表").doWrite(l);
    }

    //根据身份证查询老师信息，在将数据传给前端显示
    @Override
    public Map<String,TblTeacher> ByIdGetTeacher(MultipartFile file, TblTeacherService teacherService) {
        Map<String,TblTeacher> result = new HashMap<>();
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            List<IdData> list = EasyExcel.read(in, IdData.class, new IdExcelListener()).sheet().doReadSync();
            for(int i = 0;i<list.size();i++){
                String idNumber = list.get(i).getIdNumber();
                TblTeacher teacherInfo = teacherService.getOne(new QueryWrapper<TblTeacher>().eq("id_number", idNumber));
                result.put("student"+i+1,teacherInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
